package cn.daniellee.plugin.pr.listener;

import cn.daniellee.plugin.pr.core.RedstoneCore;
import com.plotsquared.core.location.Location;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.data.AnaloguePowerable;
import org.bukkit.block.data.Powerable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockRedstoneEvent;

import java.util.UUID;

public class BlockListener implements Listener {

	@EventHandler
	public void onBlockRedstone(BlockRedstoneEvent e) {
		org.bukkit.Location bukkitLocation = e.getBlock().getLocation();
		World world = bukkitLocation.getWorld();
		if (world != null && RedstoneCore.isPlotWorld(world.getName()) && RedstoneCore.locNotInCache(bukkitLocation)) {
			Location location = Location.at(world.getName(), bukkitLocation.getBlockX(), bukkitLocation.getBlockY(), bukkitLocation.getBlockZ());
			if (location.getOwnedPlot() != null) {
				UUID owner = location.getOwnedPlot().getOwner();
				if (owner != null) {
					OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(owner);
					if (RedstoneCore.checkPlayer(offlinePlayer)) { // (offlinePlayer.getPlayer() != null && offlinePlayer.getPlayer().hasPermission("redstone.bypass"))
						// 缓存位置
						RedstoneCore.cacheLocation(bukkitLocation);
					} else e.setNewCurrent(0);
				}
			}
		}
	}

}
